package com.signway.swmediaplayerdemo;

import static com.signway.swmediaplayerdemo.MainActivity.hideBottomUIMenu;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.mediatek.ExoMediaPlayer;
import com.signway.swmediaplayerdemo.databinding.ActivityExoBinding;
import java.io.File;

public class ExoActivity extends AppCompatActivity {
    public static final String TAG = ExoActivity.class.getName();
    private ActivityExoBinding binding;

    private SurfaceView mSurfaceView4K;
    private SurfaceView mSurfaceView2K;
    private boolean is2K = false;
    private final String VideoPath2K = Environment.getExternalStorageDirectory().getPath() + "/testmp5/2_v_25_1080.mp4";
    private final String VideoPath4K = Environment.getExternalStorageDirectory().getPath() + "/testmp4/2_v_25.mp4";

    private ExoMediaPlayer mMediaPlayer;

    private MediaPlayer.OnCompletionListener mOnCompletionListener = mp -> {
        binding.init.setVisibility(View.VISIBLE);
        switchMedia();
    };

    private MediaPlayer.OnPreparedListener mOnPreparedListener =
            mp -> mp.start();

    private MediaPlayer.OnInfoListener mOnInfoListener = (mp, what, extra) -> {
        switch (what) {
            case MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
                break;
        }
        return true;
    };

    private void switchMedia() {
        if (is2K) {
            initPlayer2K();
            is2K = false;
        } else {
            initPlayer4K();
            is2K = true;
        }
        binding.init.setVisibility(View.GONE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityExoBinding.inflate(getLayoutInflater());

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(binding.getRoot());
        hideBottomUIMenu(this);

        mMediaPlayer = new ExoMediaPlayer();
        mMediaPlayer.setSeamlessMode(true);
        mMediaPlayer.setOnPreparedListener(mOnPreparedListener);
        mMediaPlayer.setOnInfoListener(mOnInfoListener);
        mMediaPlayer.setOnCompletionListener(mOnCompletionListener);

        binding.init.setOnClickListener(v -> {
            switchMedia();
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMediaPlayer.release();
    }

    private void initPlayer4K() {
        mSurfaceView4K = new SurfaceView(this);
        mSurfaceView4K.setLayoutParams(new RelativeLayout.LayoutParams(2160, 3840));
        binding.surfaceLayout.addView(mSurfaceView4K);

        mSurfaceView4K.getHolder().setFormat(PixelFormat.TRANSPARENT);
        mSurfaceView4K.getHolder().addCallback(new SurfaceCallBack(this, mMediaPlayer));
        mSurfaceView4K.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        if (mMediaPlayer != null) {
            try {
                mMediaPlayer.reset();
                mMediaPlayer.setDataSource(this, Uri.fromFile(new File(VideoPath4K)));
                mMediaPlayer.setDisplay(mSurfaceView4K.getHolder());
                Log.d(TAG,  "mPlayer.getDuration() " + mMediaPlayer.getDuration());
            } catch (Exception e) {
                e.printStackTrace();
                mMediaPlayer.release();
                Toast.makeText(this, "无法播放", Toast.LENGTH_LONG).show();
            }
        }
    }

    private void initPlayer2K() {
        mSurfaceView2K = new SurfaceView(this);
        mSurfaceView2K.setLayoutParams(new RelativeLayout.LayoutParams(1080, 1920));
        binding.surfaceLayout.addView(mSurfaceView2K);

        mSurfaceView2K.getHolder().setFormat(PixelFormat.TRANSPARENT);
        mSurfaceView2K.getHolder().addCallback(new SurfaceCallBack(this, mMediaPlayer));
        mSurfaceView2K.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        if (mMediaPlayer != null) {
            try {
                mMediaPlayer.reset();
                mMediaPlayer.setDataSource(this, Uri.fromFile(new File(VideoPath2K)));
                mMediaPlayer.setDisplay(mSurfaceView2K.getHolder());
                Log.d(TAG,  "mPlayer.getDuration() " + mMediaPlayer.getDuration());
            } catch (Exception e) {
                e.printStackTrace();
                mMediaPlayer.release();
                Toast.makeText(this, "无法播放", Toast.LENGTH_LONG).show();
            }
        }
    }

    private class SurfaceCallBack implements SurfaceHolder.Callback {

        public SurfaceCallBack(Context context, MediaPlayer player) {
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            Log.d(TAG, "surfaceCreated " + holder.toString());
            mMediaPlayer.prepareAsync();

            if (mSurfaceView2K != null && holder == mSurfaceView2K.getHolder()) {
                if (mSurfaceView4K != null) {
                    binding.surfaceLayout.removeView(mSurfaceView4K);
                    mSurfaceView4K = null;
                }
            } else {
                if (mSurfaceView2K != null) {
                    binding.surfaceLayout.removeView(mSurfaceView2K);
                    mSurfaceView2K = null;
                }
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            Log.d(TAG, "surfaceChanged width " + width + ", height" + height);
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.d(TAG, "surfaceDestroyed");
        }
    }
}